package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Optional;

public class ProxySetGetOAD extends AxdrType {
    private ByteBuffer dataCode = null;
    private OAD soad = null;
    private Data param = null;
    private OAD road = null;
    private Unsigned8 delayTimeRead = null;

    public ProxySetGetOAD() {
    }

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OAD soad, Data param, OAD road, Unsigned8 delayTimeRead) {
        this.soad = soad;
        this.param = param;
        this.road = road;
        this.delayTimeRead = delayTimeRead;
    }

    public void setSoad(OAD soad) {
        this.soad = soad;
    }

    public void setParam(Data param) {
        this.param = param;
    }

    public void setRoad(OAD road) {
        this.road = road;
    }

    public void setDelayTimeRead(Unsigned8 delayTimeRead) {
        this.delayTimeRead = delayTimeRead;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.soad = new OAD();
        codeLength += this.soad.decode(input);
        this.param = new Data();
        codeLength += this.param.decode(input);
        this.road = new OAD();
        codeLength += this.road.decode(input);
        this.delayTimeRead = new Unsigned8();
        this.delayTimeRead.setConst();
        codeLength += this.delayTimeRead.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            while (this.dataCode.hasRemaining()) {
                output.write(this.dataCode.get());
            }
        } else {
            if (this.delayTimeRead != null && this.road != null && this.param != null && this.soad != null) {
                codeLength = 0;
                codeLength += this.delayTimeRead.encode(output);
                codeLength += this.road.encode(output);
                codeLength += this.param.encode(output);
                codeLength += this.soad.encode(output);
            } else {
                throw new IllegalStateException("delayTimeRead, road, param, or soad is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    @Override
    public String toString() {
        if (this.delayTimeRead != null && this.road != null && this.param != null && this.soad != null) {
            return "sequence: {" + "soad: " + this.soad + ", param: " + this.param + ", road: " + this.road + ", delayTimeRead: " + this.delayTimeRead + "}";
        } else {
            return "delayTimeRead, road, param, or soad is null";
        }
    }
}



